```html
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>HashMap性能优化指南 | 技术小馆</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css">
    <link rel="stylesheet" href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            color: #333;
            line-height: 1.6;
        }
        .hero {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        .card {
            transition: all 0.3s ease;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
        }
        .card:hover {
            transform: translateY(-5px);
            box-shadow: 0 10px 20px rgba(0, 0, 0, 0.15);
        }
        .highlight {
            position: relative;
        }
        .highlight::after {
            content: '';
            position: absolute;
            bottom: 0;
            left: 0;
            width: 100%;
            height: 30%;
            background-color: rgba(102, 126, 234, 0.2);
            z-index: -1;
            transition: height 0.3s ease;
        }
        .highlight:hover::after {
            height: 100%;
        }
        .drop-cap::first-letter {
            float: left;
            font-size: 3.5em;
            line-height: 0.85;
            margin: 0.1em 0.1em 0 0;
            color: #667eea;
            font-weight: 700;
        }
        footer {
            background-color: #1a1a1a;
        }
        .footer-link:hover {
            color: #667eea;
        }
        h1, h2, h3, h4 {
            font-family: 'Noto Serif SC', serif;
        }
    </style>
</head>
<body class="bg-gray-50">
    <!-- Hero Section -->
    <section class="hero text-white py-20 px-4 md:px-0">
        <div class="container mx-auto max-w-5xl text-center">
            <h1 class="text-4xl md:text-5xl font-bold mb-6 animate-fade-in">HashMap 性能优化完全指南</h1>
            <p class="text-xl md:text-2xl mb-8 opacity-90">解锁 Java 集合框架中最重要数据结构的高效使用技巧</p>
            <div class="flex justify-center space-x-4">
                <a href="#content" class="px-6 py-3 bg-white text-indigo-600 rounded-lg font-medium hover:bg-gray-100 transition duration-300">
                    <i class="fas fa-book-reader mr-2"></i>开始阅读
                </a>
                <a href="#visualization" class="px-6 py-3 border border-white text-white rounded-lg font-medium hover:bg-white hover:bg-opacity-10 transition duration-300">
                    <i class="fas fa-project-diagram mr-2"></i>可视化指南
                </a>
            </div>
        </div>
    </section>

    <!-- Main Content -->
    <main class="container mx-auto max-w-5xl px-4 py-12" id="content">
        <!-- Introduction -->
        <section class="mb-16">
            <p class="drop-cap mb-6 text-gray-700 leading-relaxed">
                HashMap 是 Java 集合框架中使用最频繁的数据结构之一，它的性能直接影响着应用程序的整体效率。本文将深入探讨 9 个关键优化策略，从初始容量设置到并发控制，帮助您充分发挥 HashMap 的性能潜力。
            </p>
            <div class="bg-indigo-50 border-l-4 border-indigo-500 p-4 my-6">
                <div class="flex items-start">
                    <div class="flex-shrink-0 text-indigo-500">
                        <i class="fas fa-lightbulb text-2xl"></i>
                    </div>
                    <div class="ml-3">
                        <p class="text-sm text-indigo-700">专业提示：这些优化技巧适用于 Java 8 及以上版本，某些特性（如红黑树转换）在早期版本中不可用。</p>
                    </div>
                </div>
            </div>
        </section>

        <!-- Optimization Cards -->
        <div class="grid md:grid-cols-2 gap-8 mb-16">
            <!-- Card 1 -->
            <div class="card bg-white rounded-xl p-6">
                <div class="flex items-center mb-4">
                    <div class="bg-indigo-100 p-3 rounded-lg mr-4">
                        <i class="fas fa-expand-arrows-alt text-indigo-600 text-xl"></i>
                    </div>
                    <h3 class="text-xl font-bold text-gray-800">1. 合理设置初始容量</h3>
                </div>
                <p class="text-gray-600 mb-4">默认初始容量为16，负载因子为0.75。预估数据量并设置合适的初始容量可以减少扩容操作，提升性能。</p>
                <div class="bg-gray-100 p-4 rounded-lg">
                    <code class="text-sm text-gray-800">// 预估1000个元素，考虑负载因子0.75<br>Map&lt;String, String&gt; map = new HashMap<>(1333);</code>
                </div>
            </div>

            <!-- Card 2 -->
            <div class="card bg-white rounded-xl p-6">
                <div class="flex items-center mb-4">
                    <div class="bg-purple-100 p-3 rounded-lg mr-4">
                        <i class="fas fa-key text-purple-600 text-xl"></i>
                    </div>
                    <h3 class="text-xl font-bold text-gray-800">2. 优化键对象设计</h3>
                </div>
                <p class="text-gray-600 mb-4">自定义对象作为键时，必须正确重写 <code>equals()</code> 和 <code>hashCode()</code> 方法，确保哈希值分布均匀。</p>
                <ul class="text-gray-600 space-y-2">
                    <li class="flex items-start">
                        <i class="fas fa-check-circle text-purple-500 mr-2 mt-1"></i>
                        <span>使用不可变对象作为键</span>
                    </li>
                    <li class="flex items-start">
                        <i class="fas fa-check-circle text-purple-500 mr-2 mt-1"></i>
                        <span>实现 <code>hashCode()</code> 时考虑所有关键字段</span>
                    </li>
                </ul>
            </div>

            <!-- Card 3 -->
            <div class="card bg-white rounded-xl p-6">
                <div class="flex items-center mb-4">
                    <div class="bg-red-100 p-3 rounded-lg mr-4">
                        <i class="fas fa-project-diagram text-red-600 text-xl"></i>
                    </div>
                    <h3 class="text-xl font-bold text-gray-800">3. 利用JDK 8+的红黑树优化</h3>
                </div>
                <p class="text-gray-600 mb-4">当链表长度超过8时，JDK 8会自动将链表转为红黑树，将查找时间复杂度从O(n)降到O(log n)。</p>
                <div class="flex justify-between items-center bg-gray-100 p-3 rounded-lg">
                    <div class="text-center">
                        <div class="text-red-500 text-lg font-bold">8</div>
                        <div class="text-xs text-gray-500">阈值</div>
                    </div>
                    <i class="fas fa-arrow-right text-gray-400"></i>
                    <div class="text-center">
                        <div class="text-red-500 text-lg font-bold">O(log n)</div>
                        <div class="text-xs text-gray-500">时间复杂度</div>
                    </div>
                </div>
            </div>

            <!-- Card 4 -->
            <div class="card bg-white rounded-xl p-6">
                <div class="flex items-center mb-4">
                    <div class="bg-blue-100 p-3 rounded-lg mr-4">
                        <i class="fas fa-sync-alt text-blue-600 text-xl"></i>
                    </div>
                    <h3 class="text-xl font-bold text-gray-800">4. 避免在遍历时修改</h3>
                </div>
                <p class="text-gray-600 mb-4">在遍历HashMap时修改内容会导致<code>ConcurrentModificationException</code>，应使用迭代器或<code>entrySet()</code>遍历。</p>
                <div class="grid grid-cols-2 gap-2">
                    <div class="bg-gray-800 text-white p-2 rounded text-sm">
                        <div class="text-green-400">// 推荐</div>
                        <div>for (Entry&lt;K,V&gt; e : map.entrySet())</div>
                    </div>
                    <div class="bg-gray-800 text-white p-2 rounded text-sm">
                        <div class="text-red-400">// 不推荐</div>
                        <div>for (K key : map.keySet())</div>
                    </div>
                </div>
            </div>
        </div>

        <!-- Additional Optimization Tips -->
        <section class="mb-16">
            <h2 class="text-2xl font-bold text-gray-800 mb-6 pb-2 border-b border-gray-200">更多优化技巧</h2>
            <div class="space-y-8">
                <!-- Tip 5 -->
                <div class="flex flex-col md:flex-row gap-6">
                    <div class="md:w-1/4">
                        <div class="bg-yellow-100 p-4 rounded-lg">
                            <h3 class="font-bold text-lg text-yellow-800 mb-2">5. 选择合适并发集合</h3>
                            <p class="text-sm text-yellow-700">多线程环境下使用<code>ConcurrentHashMap</code>替代<code>HashMap</code></p>
                        </div>
                    </div>
                    <div class="md:w-3/4 bg-white p-5 rounded-lg shadow-sm">
                        <div class="flex items-start mb-4">
                            <i class="fas fa-lock text-yellow-500 mt-1 mr-3"></i>
                            <p><code>ConcurrentHashMap</code>采用分段锁和CAS操作，在高并发场景下性能显著优于<code>Collections.synchronizedMap</code>包装的<code>HashMap</code>。</p>
                        </div>
                        <div class="bg-gray-100 p-3 rounded">
                            <code class="text-sm">Map&lt;String, String&gt; concurrentMap = new ConcurrentHashMap<>();</code>
                        </div>
                    </div>
                </div>

                <!-- Tip 6 -->
                <div class="flex flex-col md:flex-row gap-6">
                    <div class="md:w-1/4">
                        <div class="bg-green-100 p-4 rounded-lg">
                            <h3 class="font-bold text-lg text-green-800 mb-2">6. 定期清理过期数据</h3>
                            <p class="text-sm text-green-700">避免内存浪费和性能下降</p>
                        </div>
                    </div>
                    <div class="md:w-3/4 bg-white p-5 rounded-lg shadow-sm">
                        <p class="mb-3">对于生命周期有限的数据，定期清理可保持HashMap的高效性：</p>
                        <ul class="space-y-2 text-gray-700">
                            <li class="flex items-start">
                                <i class="fas fa-trash-alt text-green-500 mr-2 mt-1"></i>
                                <span>手动定时清理不再使用的条目</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-link text-green-500 mr-2 mt-1"></i>
                                <span>或使用<code>WeakHashMap</code>自动清理弱引用键</span>
                            </li>
                        </ul>
                    </div>
                </div>

                <!-- Tip 7 -->
                <div class="flex flex-col md:flex-row gap-6">
                    <div class="md:w-1/4">
                        <div class="bg-indigo-100 p-4 rounded-lg">
                            <h3 class="font-bold text-lg text-indigo-800 mb-2">7. 使用优质哈希算法</h3>
                            <p class="text-sm text-indigo-700">确保键的哈希值分布均匀</p>
                        </div>
                    </div>
                    <div class="md:w-3/4 bg-white p-5 rounded-lg shadow-sm">
                        <p class="mb-3">好的哈希算法应：</p>
                        <div class="grid md:grid-cols-2 gap-4">
                            <div class="bg-gray-100 p-3 rounded-lg">
                                <h4 class="font-medium text-indigo-600 mb-1"><i class="fas fa-thumbs-up mr-2"></i>推荐做法</h4>
                                <ul class="text-sm space-y-1">
                                    <li>使用Objects.hash()组合多个字段</li>
                                    <li>考虑使用Guava的Hashing工具类</li>
                                    <li>为不同对象类型定制哈希方法</li>
                                </ul>
                            </div>
                            <div class="bg-gray-100 p-3 rounded-lg">
                                <h4 class="font-medium text-red-600 mb-1"><i class="fas fa-thumbs-down mr-2"></i>避免做法</h4>
                                <ul class="text-sm space-y-1">
                                    <li>仅使用基本类型的哈希值</li>
                                    <li>忽略重要字段的哈希计算</li>
                                    <li>哈希计算过于复杂耗时</li>
                                </ul>
                            </div>
                        </div>
                    </div>
                </div>

                <!-- Tip 8 & 9 -->
                <div class="grid md:grid-cols-2 gap-6">
                    <!-- Tip 8 -->
                    <div class="bg-white p-5 rounded-lg shadow-sm">
                        <div class="flex items-center mb-3">
                            <div class="bg-purple-100 p-2 rounded-lg mr-3">
                                <i class="fas fa-boxes text-purple-600"></i>
                            </div>
                            <h3 class="font-bold text-lg text-gray-800">8. 批量操作优化</h3>
                        </div>
                        <p class="text-gray-700 mb-3">批量插入数据前，一次性计算并设置足够大的容量，避免多次扩容。</p>
                        <div class="bg-gray-100 p-3 rounded text-sm">
                            <code>// 批量插入10000个元素<br>Map&lt;String, String&gt; map = new HashMap<>(13334);<br>dataList.forEach(item -> map.put(item.key(), item.value()));</code>
                        </div>
                    </div>

                    <!-- Tip 9 -->
                    <div class="bg-white p-5 rounded-lg shadow-sm">
                        <div class="flex items-center mb-3">
                            <div class="bg-blue-100 p-2 rounded-lg mr-3">
                                <i class="fas fa-tachometer-alt text-blue-600"></i>
                            </div>
                            <h3 class="font-bold text-lg text-gray-800">9. 避免频繁扩容</h3>
                        </div>
                        <p class="text-gray-700 mb-3">扩容涉及rehash和数组复制，代价高昂。在数据量接近阈值时提前手动扩容。</p>
                        <div class="flex items-center justify-between bg-gradient-to-r from-blue-50 to-indigo-50 p-3 rounded">
                            <div class="text-center">
                                <div class="text-blue-600 font-bold">阈值 =</div>
                                <div class="text-sm">容量 × 负载因子</div>
                            </div>
                            <i class="fas fa-exclamation-triangle text-yellow-500 text-xl"></i>
                            <div class="text-center">
                                <div class="text-red-500 font-bold">扩容代价</div>
                                <div class="text-sm">O(n)时间/空间</div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </section>

        <!-- Visualization Section -->
        <section class="mb-16" id="visualization">
            <h2 class="text-2xl font-bold text-gray-800 mb-6 pb-2 border-b border-gray-200 flex items-center">
                <i class="fas fa-project-diagram text-indigo-500 mr-3"></i>
                HashMap优化关键点关系图
            </h2>
            <div class="bg-white p-6 rounded-lg shadow-sm">
                <div class="mermaid">
                    graph TD
                    A[HashMap性能优化] --> B[初始设置]
                    A --> C[键设计]
                    A --> D[遍历方式]
                    A --> E[并发控制]
                    A --> F[内存管理]
                    
                    B --> B1[合理初始容量]
                    B --> B2[适当负载因子]
                    
                    C --> C1[重写hashCode]
                    C --> C2[重写equals]
                    C --> C3[不可变键]
                    
                    D --> D1[使用entrySet]
                    D --> D2[避免修改遍历]
                    
                    E --> E1[ConcurrentHashMap]
                    E --> E2[分段锁]
                    
                    F --> F1[定期清理]
                    F --> F2[WeakHashMap]
                    
                    classDef opt fill:#e3f2fd,stroke:#2196f3
                    class B,C,D,E,F opt
                </div>
                <div class="mt-6 bg-gray-100 p-4 rounded-lg">
                    <h3 class="font-medium text-gray-800 mb-2"><i class="fas fa-mouse-pointer text-indigo-500 mr-2"></i>交互提示</h3>
                    <p class="text-sm text-gray-700">点击上方关系图中的节点可以查看详细信息。本图展示了HashMap性能优化的关键方面及其相互关系。</p>
                </div>
            </div>
        </section>

        <!-- Summary Section -->
        <section class="bg-indigo-50 rounded-xl p-8 mb-16">
            <h2 class="text-2xl font-bold text-indigo-800 mb-6 text-center">优化策略效果对比</h2>
            <div class="overflow-x-auto">
                <table class="min-w-full bg-white rounded-lg overflow-hidden">
                    <thead class="bg-indigo-600 text-white">
                        <tr>
                            <th class="py-3 px-4 text-left">优化策略</th>
                            <th class="py-3 px-4 text-left">适用场景</th>
                            <th class="py-3 px-4 text-left">性能提升</th>
                            <th class="py-3 px-4 text-left">实现难度</th>
                            <th class="py-3 px-4 text-left">JDK版本要求</th>
                        </tr>
                    </thead>
                    <tbody class="divide-y divide-gray-200">
                        <tr class="hover:bg-gray-50">
                            <td class="py-3 px-4">初始容量设置</td>
                            <td class="py-3 px-4">数据量可预估</td>
                            <td class="py-3 px-4 text-green-600">高</td>
                            <td class="py-3 px-4">低</td>
                            <td class="py-3 px-4">所有</td>
                        </tr>
                        <tr class="hover:bg-gray-50">
                            <td class="py-3 px-4">键对象优化</td>
                            <td class="py-3 px-4">自定义键对象</td>
                            <td class="py-3 px-4 text-green-600">高</td>
                            <td class="py-3 px-4">中</td>
                            <td class="py-3 px-4">所有</td>
                        </tr>
                        <tr class="hover:bg-gray-50">
                            <td class="py-3 px-4">红黑树转换</td>
                            <td class="py-3 px-4">高冲突场景</td>
                            <td class="py-3 px-4 text-green-600">极高</td>
                            <td class="py-3 px-4">自动</td>
                            <td class="py-3 px-4">8+</td>
                        </tr>
                        <tr class="hover:bg-gray-50">
                            <td class="py-3 px-4">并发控制</td>
                            <td class="py-3 px-4">多线程环境</td>
                            <td class="py-3 px-4 text-yellow-600">中</td>
                            <td class="py-3 px-4">低</td>
                            <td class="py-3 px-4">所有</td>
                        </tr>
                        <tr class="hover:bg-gray-50">
                            <td class="py-3 px-4">数据清理</td>
                            <td class="py-3 px-4">生命周期数据</td>
                            <td class="py-3 px-4 text-blue-600">视情况</td>
                            <td class="py-3 px-4">中</td>
                            <td class="py-3 px-4">所有</td>
                        </tr>
                    </tbody>
                </table>
            </div>
            <div class="mt-6 text-center">
                <p class="text-indigo-700">根据您的具体应用场景，选择最适合的优化组合以达到最佳性能效果</p>
            </div>
        </section>
    </main>

    <!-- Footer -->
    <footer class="py-12 text-white">
        <div class="container mx-auto max-w-5xl px-4 text-center">
            <h3 class="text-xl font-medium mb-4">技术小馆</h3>
            <p class="mb-6 text-gray-400">专注于分享前沿技术知识与实践经验</p>
            <div class="flex justify-center space-x-6">
                <a href="http://www.yuque.com/jtostring" class="footer-link text-gray-300 hover:text-white transition duration-300">
                    <i class="fas fa-book-open mr-2"></i>语雀文档
                </a>
            </div>
            <div class="mt-8 text-sm text-gray-500">
                &copy; 2023 技术小馆. 保留所有权利.
            </div>
        </div>
    </footer>

    <script>
        mermaid.initialize({
            startOnLoad: true,
            theme: 'default',
            flowchart: {
                useMaxWidth: true,
                htmlLabels: true,
                curve: 'basis'
            }
        });
        
        // 平滑滚动
        document.querySelectorAll('a[href^="#"]').forEach(anchor => {
            anchor.addEventListener('click', function (e) {
                e.preventDefault();
                document.querySelector(this.getAttribute('href')).scrollIntoView({
                    behavior: 'smooth'
                });
            });
        });
    </script>
</body>
</html>
```